﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.CodeDom.Compiler;
using NBusiness.CodeDom;

namespace NBusiness.VisualStudio.Commands
{
    class RefreshServiceRoot : CommandBase
    {
        ProjectParseInfo _parseInfo;
        EntityFile[] _files;

        public RefreshServiceRoot(ProjectParseInfo parseInfo, params EntityFile[] files)
        {
            _parseInfo = parseInfo;
            _files = files;
        }

        protected override void Execute()
        {
            EntityParser parser = null;
            EntityRoot root = null;

            lock (_parseInfo)
                parser = _parseInfo.Provider.GetParser();

            //root = parser.ParseFiles(_files);
            List<EntityElement> elements = new List<EntityElement>();
            foreach(EntityFile file in _files)
                elements.AddRange(parser.GetElements(file));

            root = parser.BuildRootFromElements(elements.ToArray());

            lock (_parseInfo)
            {
                _parseInfo.Root = root;
                _parseInfo.Elements = elements.ToArray();
            }
        }
    }
}
